import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

import Home1 from '../views/home/index.vue'
import Kind from '../views/kind/index.vue'
import Cart from '../views/cart/index.vue'
import User from '../views/user/index.vue'

Vue.use(VueRouter)

const routes = [
  // {
  //   path: '/',
  //   name: 'Home',
  //   component: Home
  // },
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // },
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: Home1
  },
  {
    path: '/kind',
    name: 'Kind',
    component: Kind
  },
  {
    path: '/cart',
    name: 'Cart',
    component: Cart
  },
  {
    path: '/user',
    name: 'User',
    component: User 
  },
  {
    path: '/detail/:proid', // 带有参数的路由
    name: 'detail',
        meta: {
          hidden: true 
        },
    component: () => import(/* webpackChunkName: "detail" */ '../views/detail/index.vue')
  },
  {
    path: '/register', // 带有参数的路由
    name: 'register',
    // 配置路由的重定向，进入该页面后，将地址改到想要跳转的路由
    redirect: '/register/step1',
    // 路由的元信息
    meta: {
      hidden: true // 不要显示底部的选项卡
    },
    component: () => import(/* webpackChunkName: "register" */ '../views/user/register/index.vue'),
    // 配置路由嵌套，子路由
    children: [
      {
        // 当路由进入到注册页面后，可以将其显示子路由
        // 路由的嵌套需要注意 children 配置的所有 path 不要加 /
        path: 'step1',
        name:"step1",
        component: () => import(/* webpackChunkName: "step1" */ '../views/user/register/step1.vue'),
        // 路由的元信息
        meta: {
          hidden: true // 不要显示底部的选项卡
        },
      },
      {
        // 当路由进入到注册页面后，可以将其显示子路由
        // 路由的嵌套需要注意 children 配置的所有 path 不要加 /
        path: 'step2',
        name:"step2",
        component: () => import(/* webpackChunkName: "step2" */ '../views/user/register/step2.vue'),
        // 路由的元信息
        meta: {
          hidden: true // 不要显示底部的选项卡
        }
      },
        {
          // 当路由进入到注册页面后，可以将其显示子路由
          // 路由的嵌套需要注意 children 配置的所有 path 不要加 /
          path: 'step3',
          name:"step3",
          component: () => import(/* webpackChunkName: "step3" */ '../views/user/register/step3.vue'),
          // 路由的元信息
          meta: {
            hidden: true // 不要显示底部的选项卡
          }
      }
    ]
  },
  {
    path: '/login', // 带有参数的路由
    meta: {
      hidden: true // 不要显示底部的选项卡
    },
    component: () => import(/* webpackChunkName: "login" */ '../views/user/login/index.vue')
  },
  {
    path: '/city', // 带有参数的路由
    meta: {
      hidden: true // 不要显示底部的选项卡
    },
    component: () => import(/* webpackChunkName: "city" */ '../views/home/city.vue')
  },
  {
    path: '/seach', // 带有参数的路由
    meta: {
      hidden: true // 不要显示底部的选项卡
    },
    component: () => import(/* webpackChunkName: "seach" */ '../views/home/seach.vue'),

  },
  {
      path: '/seach2',
      name:"seach2",
      component: () => import(/* webpackChunkName: "seach2" */ '../views/home/seach2.vue'),
      // 路由的元信息
      meta: {
        hidden: true // 不要显示底部的选项卡
      },
  },
  {
    path: '/dingdan', // 带有参数的路由
    meta: {
      hidden: true // 不要显示底部的选项卡
    },
    component: () => import(/* webpackChunkName: "dingdan" */ '../views/cart/dingdan.vue')
  },
  {
    path: '*',
    name: 'Debug',
    // component: Debug,
    component: () => import(/* webpackChunkName: "Debug" */ '../views/debug/debug.vue')
  },
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {

  const user = localStorage.getItem("userid")
  if(user){
    next()
  }else if(to.fullPath=="/cart"){
    next("/login")
  }
  else{
    next()
  }

})

export default router
